/*
 * Powered By Rubyliu
 * QQ:876181
 * Since 2012 - 2012
 */

package com.bzzoneit.newsys.web.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bzzoneit.newsys.entity.admin.SchedulerJob;
import com.bzzoneit.newsys.service.admin.SchedulerService;
import com.bzzoneit.newsys.utils.dwz.AjaxDone;
import com.bzzoneit.newsys.utils.page.Page;

/**
 * @author Ruby liu
 * @version 1.0
 * @since 1.0
 *
 */
@Controller
@RequestMapping("/scheduler")
public class SchedulerController   extends BaseController<SchedulerJob> {
	
	@Autowired
	private SchedulerService schedulerService;
	
	/**
	 * page 获得从页面传过来的分页信息
	 * @param page
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public String list(@ModelAttribute("page") Page<SchedulerJob> page,
			@ModelAttribute("scheduler") SchedulerJob scheduler,
			@RequestParam("navtabid") String navtabid,
			Model model) {
		logger.info("start  list : pageisze:"+page.getNumPerPage());
		page.setT(scheduler);
		page=schedulerService.getSchedulerList(page);
		model.addAttribute("page", page);
		model.addAttribute("scheduler", scheduler);
		model.addAttribute("navtabid", navtabid);
		return "scheduler/list";
	}
	
	/**
	 * 编辑页面
	 * @param model
	 * @param id
	 * @param navtabid
	 * @return
	 */
	@RequestMapping(value="/edit/{id}")
	public String prepare(Model model,@PathVariable("id") Long id,
			@RequestParam("navtabid") String navtabid
		) {
		model.addAttribute("navtabid", navtabid);
		model.addAttribute("scheduler", schedulerService.selectScheduler(id));
		return "scheduler/edit";
	}
	
	/**
	 * 保存更新
	 * @param scheduler
	 * @param navtabid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/update")
	public AjaxDone update(@ModelAttribute("scheduler")SchedulerJob scheduler,
			@RequestParam("navtabid") String navtabid
			) 
	{
		schedulerService.updateScheduler(scheduler);
		AjaxDone a=new AjaxDone();
		a.setStatusCode(200);
		a.setCallbackType("closeCurrent");
		a.setMessage("更新成功");
		a.setNavTabId(navtabid); //重新加载这个nav
		return a;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete/{id}")
	public AjaxDone delete(@PathVariable("id") Long id) {
		schedulerService.deleteScheduler(id);
		AjaxDone a=new AjaxDone();
		a.setStatusCode(200);
		a.setMessage("删除成功");
		return a;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/stop/{id}")
	public AjaxDone stop(@PathVariable("id") Long id) {
		AjaxDone a=new AjaxDone();
		try {
			SchedulerJob job=schedulerService.selectScheduler(id);
			schedulerService.stopJob(job.getId(),job.getName());
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.info(e.getMessage());
			a.setStatusCode(300);
			a.setMessage("停止失败");
			return  a;
		}
		a.setStatusCode(200);
		a.setMessage("停止成功");
		return a;
	}
	
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/start/{id}")
	public AjaxDone start(@PathVariable("id") Long id) {
		AjaxDone a=new AjaxDone();
		try {
			schedulerService.startJob(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			a.setStatusCode(300);
			a.setMessage("启动失败");
			return  a;
		}
		a.setStatusCode(200);
		a.setMessage("启动成功");
		return a;
	}
	/**
	 * 新增页面
	 * @param model
	 * @param navtabid
	 * @return
	 */
	@RequestMapping("/add")
	public String add(Model model,@RequestParam("navtabid") String navtabid) {
		model.addAttribute("navtabid", navtabid);
		return "scheduler/edit";
	}

	/**
	 * 保存新增
	 * @param scheduler
	 * @param navtabid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/insert")
	public AjaxDone insert(@ModelAttribute("scheduler")SchedulerJob scheduler,
			@RequestParam("navtabid") String navtabid) {
		schedulerService.insertScheduler(scheduler);
		AjaxDone a=new AjaxDone();
		a.setStatusCode(200);
		a.setCallbackType("closeCurrent");
		a.setMessage("新增成功");
		a.setForwardUrl(""); 
		a.setNavTabId(navtabid); //重新加载这个nav
		return a;
	}

}

